

/*交换机功能测试*/

#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

const std::string defaultfile = "./db/data.db";
xgmq::ExchangeManagger::ptr emp;

// 编写全局测试套件
class ExchangeTest : public testing::Environment
{
public:
    void SetUp() override
    {
        // 初始化环境
        emp = std::make_shared<xgmq::ExchangeManagger>(defaultfile);
    }
    void TearDown() override
    {
        // 清理环境
        emp->clear();
    }
};
// 新增交换机的测试
TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> mp = {{"k1", "v1"}, {"k2", "v2"}};
    emp->declareExchange("交换机1", xgmq::ExchangeType::DIRECT, true, false, mp);
    emp->declareExchange("交换机2", xgmq::ExchangeType::DIRECT, true, false, mp);
    emp->declareExchange("交换机3", xgmq::ExchangeType::DIRECT, false, false, mp);
    emp->declareExchange("交换机4", xgmq::ExchangeType::DIRECT, true, true, mp);
    emp->declareExchange("交换机5", xgmq::ExchangeType::DIRECT, true, false, mp);
    ASSERT_EQ(emp->size(),5);
}
// 删除交换机测试
TEST(exchange_test, delete_test)
{
    emp->deleteExchange("交换机1");
    emp->deleteExchange("交换机2");
    emp->deleteExchange("交换机3");
    ASSERT_EQ(emp->size(),2);
}
// 获取交换机测试
TEST(exchange_test, get_test)
{
    xgmq::ExchangePtr exp = emp->getExchange("交换机5");
    ASSERT_NE(exp.get(),nullptr);//为空就不继续往下走了
    ASSERT_EQ(exp->name,"交换机5");
    ASSERT_EQ(exp->type,xgmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->durable,true);
    ASSERT_EQ(exp->auto_delete,false);
    ASSERT_EQ(exp->getArgs(),std::string("k2=v2&k1=v1&"));


    // std::stringstream ss;
    // ss << "该交换机的名称:" << exp->name;
    // ss << "是否持久化保存:";
    // if (exp->durable == true)
    //     ss << "持久化存储\t";
    // else
    //     ss << "非持久化存储\t";

    // ss << "该交换机的类型：";
    // if (exp->type == xgmq::ExchangeType::DIRECT)
    //     ss << "直接交换";
    // else if (exp->type == xgmq::ExchangeType::FANOUT)
    //     ss << "广播交换";
    // else
    //     ss << "匹配交换";
    // std::cout << ss.str() << std::endl;
}
int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    // 添加全局测试套件
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    int ret = RUN_ALL_TESTS();

    return 0;
}